import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Link,browserHistory } from 'react-router';
import 'snack';

const TIME  = 1000;
export default function Snack({self}){
    var {snack,snackDisplay='none'} = self.state
    return (
        <div className="snack-layout" style={{display:snackDisplay}}>
            <div className='snack'>{snack}</div> 
        </div> 
    )
}
Snack.snack=function(self,msg,time,fn){
    var _this = this,
        timeType = Object.prototype.toString.call(time);

    if(timeType=='[object Undefined]'){
        time = TIME;
    }

    if(timeType=='[object Function]'){
        fn = time;
        time = TIME;
    }

    var fnType = Object.prototype.toString.call(fn)
    if(fnType != '[object Function]'){
        fn = ()=>{}
    }

    clearTimeout(this.timer)
    self.setState({snack:msg,snackDisplay:'block'});

    timeType = Object.prototype.toString.call(time);
    if(timeType=='[object Number]'){
        this.timer = setTimeout(()=>{
            self.setState({snackDisplay:'none',snack:''});
            fn()
            
        },time)
    }
    
}
Snack.close=function(self){
    clearTimeout(this.timer)
    self.setState({snackDisplay:'none',snack:''})
}
